const request=require('../../util/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //名称,规格,编号,单位,库存
    name:"",
    attr:"",
    no:"",
    unit:"",
    report_line:"",
    id:"",
    type_data:"",
    type_data_num:0,
    storage_location:"",
    type_label:"",
    //----------
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.value1 != undefined){
      this.setData({
       name:options.value1,
       attr:options.value2,
       no:options.value3,
       unit:options.value4,
       report_line:options.value5,
       id:options.value6,
       type_data:options.type_label,
       storage_location:options.storage_location,
      })
    }else{
      this.setData({
        id:0
      })
    }
  },
  bindKeyInputVant(e){
    var tmpinfo = e.detail
    var tmpname = e.currentTarget.dataset.variable
    this.data.tmpname = tmpinfo
    this.setData({
      [tmpname] : tmpinfo
    })
  },
  submit(e){
    if(this.data.name === ""){
      wx.showToast({
        icon: "error",
        title: `请输入名称`
      });
      return false;
    }else if(this.data.no === ""){
      wx.showToast({
        icon: "error",
        title: `请输入编号`
      });
      return false;
    }else if(this.data.attr === ""){
      wx.showToast({
        icon: "error",
        title: `请输入规格`
      });
      return false;
    }else if(this.data.report_line === ""){
      wx.showToast({
        icon: "error",
        title: `请输入库存警戒线`
      });
      return false;
    }
    //-----------------
    var data = {name:this.data.name,
                no:this.data.no,
                unit:this.data.unit,
                attr:this.data.attr,
                report_line:this.data.report_line,
                id:this.data.id,
                type_label:this.data.type_data_num,
                storage_location:this.data.storage_location
              }
    request.requestAjax("material/save",data,"上传中",function(res){
      const pages = getCurrentPages();
      if (pages.length >= 2) {
        const prevPage = pages[pages.length - 2];
        pages[pages.length - 2].setData({
          __returnParams: true
        }, () => {


          wx.showToast({
            title: '提交成功',
            icon: 'success',
          })
          setTimeout(() => {
            wx.navigateBack() // 确保setData回调完成后再返回
            prevPage.getList1(); // 触发上一页自定义刷新方法
          }, 2000)

            
        })
      }
    },function(res){
    })
  },

  //跳转页面
  seltype(){
    wx.navigateTo({
      url: `/pages/seltype/seltype`,
    })
  },

  setManager(){
    if (this.data.__returnParams) {
      console.log(this.data.__returnParams,"AAAAAAAAAA")
      // let valueNum = this.data.viewList[this.data.__returnParams[2]]
      // let valueid = this.data.nameToid[this.data.__returnParams[2]]
      let valueName = this.data.viewList
      let valueName1 = this.data.nameToid
      // valueNum.manager = this.data.__returnParams[1],
      // valueid.manager = Number(this.data.__returnParams[0]) 
      this.setData({ 
        viewList:valueName,
        nameToid:valueName1,
        type_data:this.data.__returnParams[1],
        type_data_num:this.data.__returnParams[0],
        __returnParams: null 
      }) // 清空避免重复触发
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  setStorage(e){
    console.log(e,"存放位置")
    this.setData({storage_location:e.detail})
  }
})